/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.araujogrosz.fractalcore;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author renatogrosz@gmail.com
 */
public class ColorMatrixImpl implements ColorMatrix {
    private List<Integer> redList;
    private List<Integer> greenList;
    private List<Integer> blueList;    
    public ColorMatrixImpl(int r, int g, int b){
        redList = new ArrayList<Integer>();
        redList.add(r);
        
        greenList = new ArrayList<Integer>();
        greenList.add(g);
        
        blueList = new ArrayList<Integer>();
        blueList.add(b);
    }
    
    private void addColor(List<Integer> listColor, int color, int distance){
        int lastItem = listColor.size() - 1;
        double lastColor = listColor.get(lastItem);
        double dColor = color - lastColor;
        double step = dColor / distance;
        for(int i = 0; i < distance ; i++){
            double stepColor = (int) (i * step);
            listColor.add((int)(lastColor + stepColor));                    
        }        
    }
    
    public void addRed(int color, int distance){
        addColor(redList,color,distance);
    }       
    
    public void addGreen(int color, int distance){
        addColor(greenList,color,distance);
    }
    
    public void addBlue(int color, int distance){
        addColor(blueList,color,distance);
    }
    
    @Override
    public Color getColor(int iteration){        
        int r = getRealColor(redList,iteration);
        int g = getRealColor(greenList,iteration);
        int b = getRealColor(blueList,iteration);        
        Color retorno = new Color(r,g,b);        
        return retorno;
    }    
    
    @Override
    public int getIntColor(int iteration){
        return getColor(iteration).getRGB();        
    }
    
    int getRealColor(List<Integer> colorList, int iteration){
        int listSize = colorList.size();
        int loopColor = iteration / colorList.size();        
        int realColor = iteration - loopColor * listSize;
        return colorList.get(realColor);        
    }
    
}
